app.controller('memberCardUpdateCtrl',['$scope','memberCardUpdateService','$state','toaster','$stateParams',
    function($scope,memberCardUpdateService,$state,toaster,$stateParams){

    $scope.memberCard={
        isFreeCarriage:0,
        isDiscount:0,
        isPresentCoupon:0,
        validTimeState:0,
        //activationState:0,
        //isPhoneVerification:0,
        //isPerfectMessage:0,
        id:$stateParams.id
    };
    $scope.findMemberCardById=function(){
        memberCardUpdateService.findMemberCard($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.memberCard=result.data;
                    if($scope.memberCard.buyCount>0){
                        $scope.memberCard.isOpenBuyCount=1;
                    }
                    if($scope.memberCard.consumePrice>0.00){
                        $scope.memberCard.isOpenConsumePrice=1;
                    }
                    if($scope.memberCard.consumeIntegral>0){
                        $scope.memberCard.isOpenConsumeIntegral=1;
                    }
                    if($scope.memberCard.type==1){
                        $scope.getValueScope();
                    }
                    break;
                case 666:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.findMemberCardById();

    //更改会员卡信息
    $scope.updateCards=function(){
        memberCardUpdateService.updateMemberCard($scope).then(function(result){
        switch (result.status){
            case 200:
                toaster.pop('success', "提示", "操作成功");
                $scope.memberCard={};
                $state.go('app.memberCard');
                break;
            case 666:
                toaster.pop('waiting', "提示", result.message);
                $state.go('app.404');
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break;
            }
        })
    };

    //获取该商家优惠券
    $scope.coupons={};
    $scope.query = function() {
        memberCardUpdateService.findCoupons($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.couponsList = result.data;
                    break;
                case 624:
                    toaster.pop('error', "操作失败", "找不到该店铺,请刷新重试");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.query();

    //查找该会员卡的取值范围
    $scope.valueScope={
        minConsumeIntegral:0,
        maxConsumeIntegral:0,
        minConsumePrice:0.00,
        maxConsumePrice:0.00,
        minBuyCount:0,
        maxBuyCount:0
    };
    $scope.getValueScope=function(){
        memberCardUpdateService.getValueScope($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.valueScope=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };
}]);
